//
//  CompanyBookViewController.m
//  lsOA
//
//  Created by 蓝水软件 on 16/9/14.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "CompanyBookViewController.h"
#import "MyInformationTableViewCell.h"
#import "AddressBookViewController.h"

@interface CompanyBookViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSArray *dataArray;
    UITableView *table;
    
}

@end

@implementation CompanyBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSWidth, KSHeight-128) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

//请求数据
- (void)getData{
    NSString *ashx = @"/Server/ContactsBook.ashx";
    NSDictionary *dic = @{@"type":@"d",@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord]}; //,@"keywords":screenDic[@"conditions"]
    [GMDCircleLoader setOnView:self.view withTitle:@"" animated:YES];
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        if (error == nil) {
            [GMDCircleLoader hideFromView:self.view animated:YES];
            NSLog(@"获取通讯录传入参数：%@",dic);
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"通讯录:%@",model);
                dataArray = model[@"data"];
                [[NSUserDefaults standardUserDefaults] setObject:model[@"data"] forKey:@"companyAddreBook"];
                [table reloadData];
                
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
            }
        }else{
            [GMDCircleLoader hideFromView:self.view animated:YES];
            dataArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"companyAddreBook"];
            if (dataArray.count > 0) {
                [XHToast showBottomWithText:@"网络异常,将为您加载本地资源" duration:4.0f];
                [table reloadData];
                return ;
            }
            [XHToast showBottomWithText:@"网络异常" duration:4.0f];
        }
    }];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellID = @"CellID";
    MyInformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (!cell) {
        cell = [[MyInformationTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setValueWithDic:dataArray[indexPath.row] withFromView:@"5" listType:@"1"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AddressBookViewController *addressBook = [[AddressBookViewController alloc]init];
    addressBook.type = @"company";
    addressBook.view.backgroundColor = [UIColor whiteColor];
    addressBook.dic = dataArray[indexPath.row];
    [self.navigationController pushViewController:addressBook animated:YES];
    
    
}


@end
